package com.brave.demo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.brave.several.teapo.pay.PayTask;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onCallPay(View v) {
        RetrofitUtils.getInstance().create(HttpService.class)
                .getPayParameter()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(String mJson) {
                        startPay(mJson);
                    }

                    @Override
                    public void onError(Throwable e) {
                        showToast(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    private void startPay(String mJson) {
        Map<String, Object> mMap = new HashMap<>();
        try {
            JSONObject obj = new JSONObject(mJson);
            Iterator<String> keys = obj.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                Object value = obj.opt(key);
                if (null != value) {
                    mMap.put(key, value);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
            showToast(e.getMessage());
        }
        if (!mMap.containsKey("state")) {
            showToast("获取数据异常");
            return;
        }
        if (!((boolean) mMap.get("state"))) {
            showToast("获取数据异常");
            return;
        }
        if (!mMap.containsKey("body")) {
            showToast("参数异常");
            return;
        }
        Object value = mMap.get("body");
        if (value instanceof JSONObject) {
            JSONObject object = (JSONObject) value;
            PayTask payTask = new PayTask(this, new Handler() {
                @Override
                public void handleMessage(@NonNull Message msg) {
                    Map<String, Object> map = (Map<String, Object>) msg.obj;
                    int code = (int) map.get("code");
                    if (code == 200) {
                        showToast("支付成功");
                    } else {
                        showToast("支付失败");
                    }
                }
            });
            payTask.pay(object.toString());
        } else {
            showToast("参数异常");
        }
    }

    private void showToast(Object obj) {
        Toast.makeText(this, String.valueOf(obj), Toast.LENGTH_LONG).show();
    }
}